var express = require('express')
var router = express.Router()
// 导入自己封装的数据库操作模块
const exec = require('../db')
//导入调试的标记
const { debug } = require('../config')

// 分页查询所有未审核的交通事件(联合gg_user表查询)
router.get('/notice', async (req, res) => {
  /*
    let { id } = req.params   // 获取:后面的参数 /:id
    const { pages, num } = req.query   // 获取?后面的参数 /messages?pages=1&num=5
    let { username, password } = req.body   // 获取请求体参数 {}
  */
  const { pages, num } = req.query
  let sql_data = `select * from gg_notice order by notice_time desc limit ${
    (pages - 1) * num
  }, ${num}`
  let sql_total = `select count(*) from gg_notice`
  try {
    let data = await exec(sql_data)
    let total = await exec(sql_total)
    res.send({
      code: 1,
      msg: '分页查询公告成功!',
      result: { total: total[0]['count(*)'], data: data },
    })
  } catch (err) {
    res.send({
      code: 0,
      msg: '分页查询公告失败!',
      result: debug ? err : '',
    })
  }
})

//获取一条公告
router.get('/notice/:id', async (req, res) => {
  //console.log(2222);
  const { id } = req.params // 获取:后面的参数 /:id
  let sql = `SELECT * from gg_notice where notice_id='${id}'`

  try {
    let data = await exec(sql)
    res.send({
      code: 0,
      msg: '获取成功!',
      result: data,
    })
  } catch (err) {
    res.send({
      code: 1,
      msg: '获取失败!',
      result: debug ? err : '',
    })
  }
})

router.post('/notice', async (req, res) => {
  /*
    let { id } = req.params   // 获取:后面的参数 /:id
    const { pages, num } = req.query   // 获取?后面的参数 /messages?pages=1&num=5
    let { username, password } = req.body   // 获取请求体参数 {}
  */
  //let { id } = req.params
  console.log(req.body)
  let { user_id, notice_title, notice_content, notice_public } = req.body // 获取请求体参数 {}
  let sql = `insert into gg_notice set user_id='${user_id}',notice_title='${notice_title}',notice_content='${notice_content}',notice_public='${notice_public}'`

  try {
    let data = await exec(sql)
    res.send({
      code: 0,
      msg: '新增成功!',
      result: data,
    })
  } catch (err) {
    res.send({
      code: 1,
      msg: '新增失败!',
      result: debug ? err : '',
    })
  }
})

//修改一条公告
router.put('/notice/:id', async (req, res) => {
  /*
    let { id } = req.params   // 获取:后面的参数 /:id
    const { pages, num } = req.query   // 获取?后面的参数 /messages?pages=1&num=5
    let { username, password } = req.body   // 获取请求体参数 {}
  */
  //console.log('put');
  const { id } = req.params
  let { user_id, notice_content, notice_public } = req.body
  //let sql = 'UPDATE gg_notice SET notice_content=作业 WHERE notice_id=3'; ${notice_content}
  let sql = `update gg_notice set user_id='${user_id}',notice_content='${notice_content}',notice_public='${notice_public}' where notice_id= '${id}'`
  try {
    let data = await exec(sql)
    res.send({
      code: 0,
      msg: '修改成功!',
      result: data,
    })
  } catch (err) {
    res.send({
      code: 1,
      msg: '修改失败!',
      result: debug ? err : '',
    })
  }
})

//删除公告
router.delete('/notice/:id', async (req, res) => {
  //console.log('del');
  //console.log('888');

  // let sql = 'DELETE FROM gg_notice WHERE notice_id=7';
  const { id } = req.params
  let sql = `delete from gg_notice where notice_id=${id}`
  try {
    let data = await exec(sql)
    res.send({
      code: 0,
      msg: '删除成功!',
      result: data,
    })
  } catch (err) {
    res.send({
      code: 1,
      msg: '删除失败!',
      result: debug ? err : '',
    })
  }
})

module.exports = router
